package com.bdmc.base.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import com.bdmc.base.domain.BaseCar;
import com.bdmc.base.domain.BaseDevice;
import com.bdmc.base.service.IBaseDeviceService;
import com.bdmc.common.core.page.TableDataInfo;
import com.bdmc.common.core.domain.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.bdmc.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.bdmc.common.utils.StringUtils;
import com.bdmc.base.domain.bo.BasePersonBo;
import com.bdmc.base.domain.vo.BasePersonVo;
import com.bdmc.base.domain.BasePerson;
import com.bdmc.base.mapper.BasePersonMapper;
import com.bdmc.base.service.IBasePersonService;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 人员Service业务层处理
 *
 * @author bdmc
 * @date 2022-03-03
 */
@Service
public class BasePersonServiceImpl extends ServicePlusImpl<BasePersonMapper, BasePerson, BasePersonVo> implements IBasePersonService {

    @Autowired
    IBaseDeviceService deviceService;

    @Override
    public BasePersonVo queryById(Long personId) {
        return getVoById(personId);
    }

    @Override
    public TableDataInfo<BasePersonVo> queryPageList(BasePersonBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<BasePerson> lqw = buildQueryWrapper(bo);
        Page<BasePersonVo> result = pageVo(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    @Override
    public TableDataInfo<BasePersonVo> mySelectPage(BasePersonBo bo, PageQuery pageQuery) {
        return TableDataInfo.build(baseMapper.mySelectPage(pageQuery.build(), bo));
    }

    @Override
    public List<BasePersonVo> queryList(BasePersonBo bo) {
        return baseMapper.mySelectPage(bo);
    }

    private LambdaQueryWrapper<BasePerson> buildQueryWrapper(BasePersonBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BasePerson> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getDeptId() != null, BasePerson::getDeptId, bo.getDeptId());
        lqw.like(StringUtils.isNotBlank(bo.getPersonName()), BasePerson::getPersonName, bo.getPersonName());
        lqw.eq(StringUtils.isNotBlank(bo.getPersonType()), BasePerson::getPersonType, bo.getPersonType());
        lqw.eq(StringUtils.isNotBlank(bo.getPostName()), BasePerson::getPostName, bo.getPostName());
        lqw.eq(StringUtils.isNotBlank(bo.getIdcard()), BasePerson::getIdcard, bo.getIdcard());
        return lqw;
    }

    @Override
    public Boolean insertByBo(BasePersonBo bo) {
        BasePerson add = BeanUtil.toBean(bo, BasePerson.class);
        validEntityBeforeSave(add);
        boolean flag = save(add);
        if (flag) {
            bo.setPersonId(add.getPersonId());
        }
        return flag;
    }

    @Override
    public Boolean updateByBo(BasePersonBo bo) {
        BasePerson update = BeanUtil.toBean(bo, BasePerson.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(BasePerson entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }

    @Transactional
    @Override
    public Boolean bindDevice(BasePersonBo bo) {
        BasePerson person = getById(bo.getPersonId());
        Assert.notNull(person, "未找到待绑定人员");
        BaseDevice device = deviceService.getById(bo.getDeviceId());
        Assert.notNull(device, "未找到指定设备");
        synchronized (this) {
            long count = count(new LambdaQueryWrapper<BasePerson>().eq(BasePerson::getDeviceId, bo.getDeviceId()));
            Assert.isTrue(count == 0, "设备已被绑定，请选择其他设备");
            person.setDeviceId(bo.getDeviceId());
            updateById(person);
            device.setBind(1);
            device.setBindTarget(person.getPersonName());
            deviceService.updateById(device);
            return true;
        }
    }

    @Transactional
    @Override
    public Boolean unbindDevice(BasePersonBo bo) {
        BasePerson person = getById(bo.getPersonId());
        Assert.notNull(person, "未找到待绑定人员");
        Assert.isTrue(bo.getDeviceId() != null&&bo.getDeviceId()>0 && person.getDeviceId().equals(bo.getDeviceId()), "参数不正确");
        synchronized (this) {
            BaseDevice device = deviceService.getById(bo.getDeviceId());
            person.setDeviceId(0L);
            updateById(person);
            device.setBind(0);
            device.setBindTarget("");
            deviceService.updateById(device);
            return true;
        }
    }

    @Override
    public BasePersonVo getByDeviceId(Long deviceId) {
        LambdaQueryWrapper<BasePerson> lqw = Wrappers.lambdaQuery();
        lqw.eq(BasePerson::getDeviceId,deviceId).last("limit 1");
        return getVoOne(lqw);
    }
}
